
**********************************************
*** Program to Produce Figures 2 of Appendix *
**********************************************

clear
drop _all
set more off

cd "$US_Ineq_Repl"
local Processed = "$US_Ineq_Repl/Processed"
local FFL = "$US_Ineq_Repl/Scripts/FFL"

// customize your file path
cd "`FFL'"
global data "`c(pwd)'\Data"
global figure "`c(pwd)'\Figures"

*** define program to run unconditional quantile regression using RIF ***

capture program drop rifuqr	
program define rifuqr
	local qt=`1'		// quantile of interest
	local qc=`qt'/100
	local width=`2'		// kernel density bandwidth
	local expvar `3'	// RIF expvar: call with double quotes
		
	* calculate density
	pctile valx=lrwage3 [aweight=orgwgt], nq(100)	// use imputed wages if top-coded
	kdensity lrwage3 [aweight=orgwgt], at(valx) gen(evalt denst) ///
		width(`width') nograph	// find density at desired grid
	
	* calculate RIF
	gen rif_`qt'=evalt[`qt']+`qc'/denst[`qt'] if lrwage3>=evalt[`qt']  
	replace rif_`qt'=evalt[`qt']-(1-`qc')/denst[`qt'] if lrwage3<evalt[`qt'] 
	drop valx evalt denst
	
	* RIF regression
	reg rif_`qt' `expvar' [aweight=orgwgt]
end

*** run regression & save estimates ***

global expvar union pubsect manuf nonwhite female partte married smsa ///
	exper exper2 expe2 expe3 expe4 ///
    educ2 educ3 educ4 educ5 edex ee2 ee3 ee4 ee5 ee6 ee7 ee9 ee10 ee11 ee13 ee14 ee15 ///
    indu2 indu3 indu4 indu5 indu6 indu7 indu8 indu9 indu10 indu11 indu12 indu13 indu14 ///
	indu15 indu16 indu17 indu18 indu19 indu20 ///
	state2 state3 state4 state5 state6 state7 state8 state9 ///
	state10 state11 state12 state13 state14 state15 state16 ///
	state17  state18 state19 state20 state21 state22 state23 ///
	state24 state25 state26 state27 state28 state29 state30 ///
	state31 state32 state33 state34 state35 state36 state37 ///
	state38 state39 state40 state42 state43 state44 state45 ///
	state46 state47 state48 state49 state50 state51

matrix drop _all

foreach time in 86 15 {
	
	use $data\us`time'.dta , clear 	
	forvalues qt=5(5)95 {
		rifuqr `qt' 0.06 "$expvar"
		matrix T`time'Q`qt'=(`qt',e(b))   
		matrix B`time'=(nullmat(B`time')\T`time'Q`qt')
			// accumulate quantile and coefficients
	}

}

local time=86
local names : colfullnames B`time'
foreach name of local names {
	local newnames`time' `newnames`time'' `= subinstr("`name'", "_" , "" , .)'
}
matrix colnames B`time' = `newnames`time''
matrix colnames B15 = `newnames`time''

foreach time in 86 15 {
	clear
	svmat double B`time',  names(col)  // export quantile and coefficients
	save $data\temp`time'.dta , replace	
}	
	

*** format ***

* combine results
use $data\temp86.dta , clear
gen time=0
append using $data\temp15.dta
replace time=1 if time==.

rename c1 qtau
replace qtau=qtau/100

*** plot ***
set scheme s1color
global option xtitle("Quantile") xlabel(0(.2)1) ytitle("") legend(off)

cd "$figure\coef"

label var union "Unionization"
label var pubsect "Public Sector"
label var manuf "Manufacturing Sector"
label var nonwhite "Nonwhite"
label var female "Women"
label var partte "Part Time"
label var married "Married"
label var smsa "Urban"
label var exper "Experience"
label var educ2 "High School"
label var educ3 "Some College"
label var educ4 "Associate Degree"
label var educ5 "College Degree"

local variable = "union"
local title: var label `variable'
  graph twoway (connected `variable' qtau if time==1, m(t) clw(medthick) lp(solid) mc(blue) lc(blue)) ///
	(connected `variable' qtau if time==0, m(o) clw(medthick) lp(longdash) mc(red) lc(red)), ///
	xtitle("Quantile") xlabel(0(.2)1) ytitle("") yline(0) subtitle("`title'") ///
	legend(ring(0) position(8) bmargin(large) col(1))legend(ring(0) position(8) bmargin(large) ///
	order(2 1) lab(1 "2015") lab(2 "1986") region(lstyle(none)) ) ///
	saving(`variable',replace) nodraw
	
foreach var in pubsect manuf nonwhite female partte married smsa exper ///
			educ2 educ3 educ4 educ5 {
  local title: var label `var'
  graph twoway (connected `var' qtau if time==1, m(t) clw(medthick) lp(solid) mc(blue) lc(blue)) ///
	(connected `var' qtau if time==0, m(o) clw(medthick) lp(longdash) mc(red) lc(red)) ///
	, $option yline(0) subtitle("`title'") saving(`var',replace) nodraw
}

*** combine & export ***

graph combine union.gph pubsect.gph manuf.gph ///
			  partte.gph nonwhite.gph female.gph ///
			  married.gph smsa.gph educ2.gph ///
			  educ3.gph educ4.gph educ5.gph ///
			  , altshrink col(3) xsize(12) ysize(15) saving($figure\fig2.gph, replace)
graph export $figure\fig2.pdf , replace

graph export $US_Ineq_Repl\Results\Figures\FigF2.png , replace

rm $data\temp86.dta
rm $data\temp15.dta


